package com.ColonyCount.View;

import java.util.ArrayList;
import java.util.List;

import com.ColonyCount.OnThresholdChangedListener;
import com.ColonyCount.OnViewChangedListener;
import com.ColonyCount.Image.Histogram;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;

public class CountHistogramView extends View {
	
	List<OnViewChangedListener> thresholdChangedListeners = new ArrayList<OnViewChangedListener>();
	
	int threshold = 255;

	boolean movingMax = false;
	float lastX;

	int lineTouchRadius;
    int histogramHeight; 
    int histogramWidth;
	int margin = 5;
	int midmargin = 1;
    int colorBarHeight = 6 ;
	int width;
	int height;
    	
    int[] values;
    int maxValue;
    
	public CountHistogramView(Context context) 
	{
		super(context);
        initialize();
	}
	
	public CountHistogramView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        initialize();
    }

	void initialize()
	{
	    DisplayMetrics dm = getResources().getDisplayMetrics();
	    lineTouchRadius = (int)((float)dm.densityDpi*0.2f); 
	}
	
	public void registerThresholdChangedListener(OnViewChangedListener handler)
	{
		thresholdChangedListeners.add(handler);
	}
	
	public void unregisterThresholdChangedListener(OnViewChangedListener handler)
	{
		thresholdChangedListeners.remove(handler);
	}

	public void setValues(int[] values, int maxValue)
	{
		this.values = values;
		this.maxValue = maxValue;
		invalidate();
	}

	@Override
	protected void onDraw (Canvas canvas)
	{
		if(values == null)
			return;
		
		height = canvas.getHeight();
		width = canvas.getWidth();
		
		histogramHeight = height - 2*margin - midmargin - colorBarHeight;
	    
	    histogramWidth = width - 2*margin;	
		
		
		Paint mPaintBackground = new Paint();
		mPaintBackground.setStyle(Paint.Style.FILL);
		mPaintBackground.setColor(Color.LTGRAY);

		canvas.drawRect(0,0,width, height, mPaintBackground);
			
		// Histogram
		int startX = margin;
		int histogramY = histogramHeight + margin;
		int colorBarY = histogramY + midmargin + colorBarHeight;
		float heightFactor = (float)histogramHeight/(float)maxValue;
		float widthFactor = (float)histogramWidth/255;
		widthFactor = 1;
		Paint mPaintHistogram = new Paint();
		mPaintHistogram.setStyle(Paint.Style.FILL);
		
		
		
		for(int i=0; i < values.length; i++)
		{
			mPaintHistogram.setColor(Color.BLACK);
			int x = (int)((float)i*widthFactor) + startX;
			int barHeight = (int) Math.ceil((float)(values[i])*heightFactor);
			
			int endX = x;
			int endY = histogramY - barHeight;
			
			canvas.drawLine(x, histogramY, endX, endY, mPaintHistogram);

			int color = 0xFF000000 | i << 16 | i << 8 | i;
			mPaintHistogram.setColor(color);
			canvas.drawLine(x, colorBarY, x, colorBarY - colorBarHeight, mPaintHistogram);
		}
		 
//		// Threshold lines
//		Paint mPaintThreshold = new Paint();
//		mPaintThreshold.setStyle(Paint.Style.FILL);
//		mPaintThreshold.setColor(Color.GREEN);
//		mPaintThreshold.setStrokeWidth(3);
//		
//		int min = 0 + margin;
//		int max = threshold + margin;
//
//		// Draw selection line
//		canvas.drawLine(max, margin, max, height - margin, mPaintThreshold);
//		
//		// Draw overlay
//		mPaintThreshold.setAlpha(60);
//		canvas.drawRect(min, margin, max, height - margin, mPaintThreshold);
//	
//		Paint mPaintText = new Paint();
//		mPaintText.setStyle(Paint.Style.FILL);
//		mPaintText.setColor(Color.WHITE);
//		mPaintText.setTextSize(10);
//		canvas.drawText("" + threshold, max, 18, mPaintText);
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		
		//if(v == (ImageView) findViewById(R.id.histogramView))
		{
			float x = event.getX() - margin;
			switch(event.getAction())
			{
			case MotionEvent.ACTION_DOWN:
				
				if(Math.abs(threshold - (int)x) < lineTouchRadius)
				{
					movingMax = true;
					break;
				}
				
				break;
			case MotionEvent.ACTION_MOVE:
				float delta = x - lastX;
				if(movingMax)
				{
					threshold += (int)delta;
					threshold = clamp(threshold, 0, 255);
				}			
				
				ThresholdChanged(threshold);
				invalidate();
				break;
			case MotionEvent.ACTION_UP:
				movingMax = false;
				ThresholdChanged(threshold);
				invalidate();
				break;
			}
			lastX = x;
			return true;
		}
		//return false;
	}
	
	void ThresholdChanged(int threshold)
	{
		for(OnViewChangedListener listener : thresholdChangedListeners)
		{
			listener.onViewChanged(this);
		}
	}
	
	int clamp(int value, int min, int max)
	{
		if(value > max)
			return max;
		if(value < min)
			return min;
		return value;
	}
}
